﻿// owner 蒋家伟

#include "GenericElement.h"

// jsdk
#include "ObjectReflectDefine.h"
#include "Dbg_Warn_Define.h"

using namespace jsdk;

OBJECT_REFLECT_DEFINE(jsdk, GenericElement):
    m_opElementBaseCompoent(ElementBaseCompoent::create()),
    m_opExternalData(nullptr)
{
}

jsdk::OwnerPtr<jsdk::GenericElement> jsdk::GenericElement::create(const UniqueId& typeUid, OwnerPtr<IExternalData> opExternalData, bool isTemp)
{
    OwnerPtr<jsdk::GenericElement> opGenericElement = NEW_AS_OWNERPTR(jsdk::GenericElement);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGenericElement, L"GenericElement对象创建失败", L"蒋家伟", L"2023-12-19");

    // 1、公共数据
    jsdk::ElementBaseCompoent* pElementBaseCompoent = opGenericElement->getElementBaseCompoent();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    pElementBaseCompoent->setTypeUid(typeUid);
    pElementBaseCompoent->setIsTemporary(isTemp);

    // 2、外部数据
    opGenericElement->setopExternalData__(transformOwnerShip(opExternalData));

    return transformOwnerShip(opGenericElement);
}

void jsdk::GenericElement::setDocument(IDocument* pDoc)
{
    ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    pElementBaseCompoent->setDocument(pDoc);
}

void jsdk::GenericElement::setElementId(const ElementId& elementId)
{
    ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    pElementBaseCompoent->setElementId(elementId);
}

void jsdk::GenericElement::setComponentOwnerElement()
{
    // 1、ElementBaseCompoent
    ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    pElementBaseCompoent->setComponentOwnerElement(this);

    // 2、外部数据
    if (jsdk::IExternalData* pExternalData = getExternalData())
    {
        pExternalData->setOwnerElement(this);
    }
}

const jsdk::IDocument* jsdk::GenericElement::getDocument() const
{
    const ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    return pElementBaseCompoent->getDocument();
}

jsdk::IDocument* jsdk::GenericElement::getDocument()
{
    ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    return pElementBaseCompoent->getDocument();
}

jsdk::ElementId jsdk::GenericElement::getElementId() const
{
    const ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_UNLESS(pElementBaseCompoent, jsdk::ElementId::s_InvalidId ,L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    return pElementBaseCompoent->getElementId();
}

const jsdk::IElementBasicInfomation* jsdk::GenericElement::getBasicInfomation() const
{
    const ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    return pElementBaseCompoent->getBasicInfomation();
}

jsdk::IElementBasicInfomation* jsdk::GenericElement::getBasicInfomation()
{
    ElementBaseCompoent* pElementBaseCompoent = getElementBaseCompoent();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementBaseCompoent, L"通用对象缺少公共组件", L"蒋家伟", L"2023-12-20");
    return pElementBaseCompoent->getBasicInfomation();
}

const jsdk::IExternalData* jsdk::GenericElement::getExternalData() const
{
    return getopExternalData__().get();
}

jsdk::IExternalData* jsdk::GenericElement::getExternalData()
{
    return getopExternalDataFW__().get();
}

jsdk::ElementBaseCompoent* jsdk::GenericElement::getElementBaseCompoent()
{
    return getopElementBaseCompoentFW__().get();
}

const jsdk::ElementBaseCompoent* jsdk::GenericElement::getElementBaseCompoent() const
{
    return getopElementBaseCompoent__().get();
}
